package com.jketing.controller.ams.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jketing.controller.BaseController;
import com.jketing.entity.ams.sys.Function;
import com.jketing.service.ams.sys.FunctionService;

/**
 * Controller 请求处理
 * 
 * @author  李荣华
 * @version v1.0
 * @date    2017-11-5
 * @since   Spring-mvc 4.3.8
 */
@Controller
@RequestMapping("/function")
public class FunctionController extends BaseController<Function,String>{

	/**
	 *  service 服务层引用
	 */
	private FunctionService service;
	
	/**
	 *  service 自动注入服务层操作实例
	 */
	@Autowired
	public void setService(FunctionService service) {
		super.setBaseService(service);
		this.service = service;
	}
	
	@Override
	protected String getPrefixPath() {
		return "sys/function/";
	}

	@Override
	protected String getQueryMode() {
		return "Entity";
	}
	
	/**
	 * 初始化多选对话框
	 */
	@Override
	protected void initMultiSelect(Map<String, Object> map) {
		map.put("funTree",service.getTree(service.getRootId()).toString());
	}
	
	/**
	 * 初始化列表框架页
	 */
	@Override
	protected void initListFrame(Map<String, Object> map) {
		map.put("topTree", service.getTree((String)map.get("rootId")));//加载从id为1的根节点开始
	}
	
	@Override
	protected Function getQuery(Function query) {
		String nodeType = request.getParameter("nodeType");
		String type = (String)getSession().getAttribute("region_type");
		if("redirect".equals(type)) {
			request.setAttribute("type", type);			 
			nodeType = getSession().getAttribute("nodeType")!=null?
					getSession().getAttribute("nodeType").toString():null;
			getSession().removeAttribute("nodeType");
			if("Entity".equals(getQueryMode())) {
				query = (Function)getSession().getAttribute("region_query");
			}
		}
		request.setAttribute("nodeType",nodeType);
		//可在此处增加修改查询条件的代码
		return query;
	}
	
	/**
	 * 异步请求树型子节点，返回json数据
	 * 
	 * produces="application/json; charset=utf-8" 解决单方法返回中文乱码问题
	 * 
	 * @return 无返回，直接向response里写数据
	 */
	@RequestMapping(value="/getSubTree",method=RequestMethod.GET)
	@ResponseBody
	public String getSubTree(String id){
		return service.getSubTree(id).toString();
	}
	
	/**
	 *  初始化新增编辑页
	 */
	@Override
	protected void initAdd(Map<String, Object> map) {
		String order = request.getParameter("order");
		String nodeType = request.getParameter("nodeType");
		String parentId = request.getParameter("parentId");
		Function parent = new Function();
		parent.setId(parentId);
		Function entity = new Function();
		entity.setParent(parent);
		map.put("nodeType", nodeType);
		map.put("entity", entity);
		List<Function> list = new ArrayList<Function>();
		list.add(service.getOne(parentId));
		map.put("funList", list);
		//新增后返回所选树型节点列表
		getSession().setAttribute("function_type", "redirect");
		getSession().setAttribute("function_query", entity);
		getSession().setAttribute("function_order", order);
		getSession().setAttribute("nodeType", nodeType);
	}
	
	/**
	 *  初始化更新编辑页
	 */
	@Override
	protected void initEdit(Map<String, Object> map) {
		String nodeType = request.getParameter("nodeType");
		Function entity = (Function)map.get("entity");
		List<Function> list = new ArrayList<Function>();
		list.add(entity.getParent());
		map.put("funList", list);
		getSession().setAttribute("nodeType", nodeType);
	}
	
	@Override
	protected void beforeDeleting(List<String> ids) {
		String nodeType = request.getParameter("nodeType");
		getSession().setAttribute("nodeType", nodeType);
	}
}
